<?php
/**
 * gpa_new Description
 * @package
 * @name URI
 * @filesource URI.php
 * @author Meta Sanjaya
 */

class URI
{
    private $_app = '';
    private $_act = '';
    private $_param = array();

    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        $this->reroute();
    }

    private function reroute()
    {
        $route = str_replace(explode("/", $_SERVER['SCRIPT_NAME']), "", $_SERVER['REQUEST_URI']);
        $route = str_replace(strstr($route, '?'), "", $route);
        $route = str_replace(strstr($route, '#'), "", $route);
        
        if(!empty($route))
        {
            $x = array();
            $array = explode('/', $route);
            for($i=0,$j=count($array);$i<$j;$i++) {
                if(!is_null($array[$i])&&!empty($array[$i])) {
                    $x[] = $array[$i];
                }
            }
            $array = $x;
            if(isset($array[0])) {
                $this->_app = strtolower(array_shift($array));

            }
            if(isset($array[0])) {
                $this->_act = strtolower(array_shift($array));
            }

            $this->_param = $array;
        }

        if($_SERVER['QUERY_STRING'] != '' || $this->_app == '' || $this->_act == '') {
            if(isset($_GET['app'])) {
                $this->_app = strtolower($_GET['app']);
            }
            if(isset($_GET['act'])) {
                $this->_act = strtolower($_GET['act']);
            }
        }

        if($this->_app=='') {
            $this->_app = getInstance('config')->get('default_app');
        }
        
        if($this->_act=='') {
            $this->_act = '__default';
        }
    }

    public function getApp()
    {
        return $this->_app;
    }

    public function getAct()
    {
        return $this->_act;
    }

    public function getParam($type='array')
    {
        if($type=='array') {
            return $this->_param;
        } else {
            $return = array();
            for($i=0,$j=count($this->_param);$i<$j;$i++) {
                if($i%2) {
                    $return[$this->_param[$i-1]] = $this->_param[$i];
                } else {
                    $return[$this->_param[$i]] = null;
                }
            }
            return $return;
        }
    }
}

function redirect($url="", $addHost=true) {
    header("location: " . ($addHost?getInstance('config')->get('http_host'):'') . $url, true);
    exit;
}

function getHttpUrl()
{
    return System::get('config')->get('http_host');
}

function getResourceUrl()
{
    return getHttpUrl() . 'resources/';
}

function getImgUrl()
{
    return getResourceUrl() . 'images/';
}

function getCssUrl()
{
    return getResourceUrl() . 'css/';
}

function getJsUrl()
{
    return getResourceUrl() . 'js/';
}

function getAppImgUrl($app='')
{
    return getImgUrl() . (!empty($app)?$app:getInstance('uri')->getApp()) . '/';
}

function getAppCssUrl($app='')
{
    return getCssUrl() . (!empty($app)?$app:getInstance('uri')->getApp()) . '/';
}

function getAppJsUrl($app='')
{
    return getJsUrl() . (!empty($app)?$app:getInstance('uri')->getApp()) . '/';
}

function getAppUrl($app='',$act='')
{
    return getHttpUrl() . (empty($app) ? getInstance('uri')->getApp() : $app) . '/' . (empty($act) ? getInstance('uri')->getAct() : $act);
}

function getAppPath($app='')
{
    return(empty($app) ? __APP__ . getInstance('uri')->getApp() : __APP__ . $app . DIRECTORY_SEPARATOR);
}

function getImgPath()
{
    return __ROOT__ . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
}

function getAppImgPath($app='')
{
    return getImgPath() . (empty($app) ? getInstance('uri')->getApp() : $app) . DIRECTORY_SEPARATOR;
}

?>